<?php

namespace catchAdmin\stock\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\stock\model\Box as BoxModel;
use think\Response;
use think\facade\Db;
use catcher\CatchAuth;
class Box extends CatchController
{
    
    protected $boxModel;
    
    /**
     *
     * @time 2022/01/23 13:53
     * @param BoxModel $boxModel
     * @return mixed
     */
    public function __construct(BoxModel $boxModel)
    {
        $this->boxModel = $boxModel;
    }
    
    /**
     *
     * @time 2022/01/23 13:53
     * @param Request $request
     * @return Response
     */
    public function index(CatchAuth $auth,Request $request) : Response
    {

         $tray_id = $request->post('tray_id');
         $box = DB::name('db_box')->where('tray_id',$tray_id)->count();
         $user = $auth->user();
         if($box === 0){
             $info = $this->create($user['tenant_id'],$tray_id);
             if($info['code'] === 0){
                return CatchResponse::fail('拆托失败');
             }
         }
         return CatchResponse::paginate($this->boxModel->getList());
    }
    
    /**
     *
     * @time 2022/01/23 13:53
     * @param Request $request
     * @return array
     */
    public function create($tenant_id,$tray_id)
    {
        $stock = DB::name('db_stock')->field('tray_id,box_number,packing_num,number,config_id,Good_Qty,Hold_Qty,NG_Qty')->where('tray_id',$tray_id)->find();

        if($stock['Good_Qty'] > 0){
            $Qty = 'Good_Qty';

        }elseif($stock['Hold_Qty'] > 0){
            $Qty = 'Hold_Qty';

        }elseif($stock['NG_Qty'] > 0){
            $Qty = 'Hold_Qty';

        }

        for ($i=1; $i<=$stock['box_number']; $i++) {
            $params[$i]['tray_id'] = $stock['tray_id'];
            $params[$i]['config_id'] = $stock['config_id'];
            $params[$i]['tenant_id'] = $tenant_id;
            $params[$i]['box_id'] = date('YmdHis').mt_rand(1000,9999);
            if(intval($stock['number']) >= intval($stock['packing_num'])){
                if($i == intval($stock['box_number'])){
                    $params[$i][$Qty] = $stock['number'];
                }else{
                    $params[$i][$Qty] = $stock['packing_num'];
                }
                $stock['number'] = intval($stock['number'])-intval($stock['packing_num']);

            }elseif($stock['number'] > 0){
                $params[$i][$Qty] = $stock['number'];
                $stock['number'] = intval($stock['number'])-intval($stock['number']);

            }else{
                $params[$i][$Qty] = 0;
            }
        }
        Db::startTrans();
        try {
            $this->boxModel->insertAllBy($params);
            DB::name('db_stock')->where('tray_id',$tray_id)->update(['split_status'=>1]);
            Db::commit();
            return array('code'=>1);
        } catch (\Exception $exception) {
            Db::rollback();
            return array('code'=>0);
        }
    }
    
    /**
     *
     * @time 2022/01/23 13:53
     * @param $id
     * @return Response
     */
    public function read($id) : Response
    {
        return CatchResponse::success($this->boxModel->findBy($id));
    }
    
    /**
     *
     * @time 2022/01/23 13:53
     * @param $id
     * @param Request $request
     * @return Response
     */
    public function update(Request $request) : Response
    {
        $params = $request->post();
        $where['tray_id'] = $params['tray_id'];

        $stock = DB::name('db_stock')->where($where)->value('number');

        // $where['id'] = ['<>',$params['id']];
        $box = DB::name('db_box')->field('SUM(Good_Qty) as Good_Qty,SUM(Hold_Qty) as Hold_Qty,SUM(NG_Qty) as NG_Qty')->where($where)->where('id','<>',$params['id'])->find();

        $Good_Qty = intval($box['Good_Qty']) + intval($params['Good_Qty']);
        $Hold_Qty = intval($box['Hold_Qty']) + intval($params['Hold_Qty']);
        $NG_Qty = intval($box['NG_Qty']) + intval($params['NG_Qty']);
        
        if(($Good_Qty + $Hold_Qty + $NG_Qty)  > $stock){
            return CatchResponse::fail('数量大于仓库数量，请核对数量');
        }

        Db::startTrans();
        try {
            $this->boxModel->updateBy($params['id'], $request->post());
            DB::name('db_stock')->where('tray_id',$params['tray_id'])->update([
                        'Good_Qty'   => $Good_Qty,
                        'Hold_Qty'   => $Hold_Qty,
                        'NG_Qty'     => $NG_Qty,
                    ]);
            Db::commit();
            return CatchResponse::success('修改成功');
        } catch (\Exception $exception) {
            Db::rollback();
            return CatchResponse::fail('修改失败');
        }
    }
    
    /**
     *
     * @time 2022/01/23 13:53
     * @param $id
     * @return Response
     */
    public function delete($id) : Response
    {
        return CatchResponse::success($this->boxModel->deleteBy($id));
    }
}